package com.hjw.dao;

import com.hjw.pojo.DVD;

import java.time.LocalDate;
import java.util.Arrays;

/**
 * @author hjwStrart
 * @create 2022-01-14 11:07
 */
public class DVDDaoImp implements DVDDao{
    private static DVD[] dvds = new DVD[5];
    static{
        dvds[0] = new DVD("罗马假日",1,LocalDate.of(2021,1,14),15L,null,5.0);
        dvds[1] = new DVD("风声鹤唳",0,null,12L,null,6.5);
        dvds[2] = new DVD("浪漫满屋",0,null,30L,null,4.8);
        dvds[3] = new DVD("hjw自传",0,null,999L,null,6.0);
    }

    //先不考虑扩容问题
    @Override
    public Boolean addDVD(DVD dvd) {
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i] == null) {
                dvds[i] = dvd;
                return true;
            }
        }
        return false;
    }

    @Override
    public DVD[] queryAllDVD() {
        DVD[] newArr = dvds;
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i] == null ){
                newArr = Arrays.copyOf(DVDDaoImp.dvds, i);
                break;
            }
        }
        return newArr;
    }

    @Override
    public DVD queryOneDVD(String name) {
        if (name == null || "".equals(name))
            return null;
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i].getName().equals(name)){
                return dvds[i];
            }
        }
        return null;
    }

    @Override
    public Boolean loanDVD(String name,LocalDate localDate) {
        DVD dvd = queryOneDVD(name);
        if (dvd == null){
            return false;
        }else{
//            判断该DVD是否已被借出
            if (dvd.getStatus() == 1){
                return false;
            }
//            将DVD状态设为借出状态,并设置借出时间，将归还时间置空,将借出次数加1
            dvd.setStatus(1);
            dvd.setLoanTime(localDate);
            dvd.setReturnTime(null);
            dvd.setLoanCount(dvd.getLoanCount() + 1);
            return true;
        }
    }

    @Override
    public Boolean returnDVD(String name,LocalDate localDate) {
        DVD dvd = queryOneDVD(name);
        if (dvd == null){
            return false;
        }else{
//            判断DVD是否已被归还
            if (dvd.getStatus() == 0){
                return false;
            }
//            将DVD状态设为未借出状态,并设置归还时间
            dvd.setStatus(0);
            dvd.setReturnTime(localDate);
            return true;
        }
    }


    @Override
    public Boolean deleteDVD(String name) {
        if (name == null || "".equals(name)) {
            return false;
        }
//        获取dvds中一个null的索引
        Integer integer = returnIndex();
//        遍历dvds
        for (int i = 0; i < (integer == null ? dvds.length : integer); i++) {
//            判断dvds中是否存在名为name的DVD
            if (dvds[i].getName().equals(name)){
//                  若存在名为name的DVD，就将此DVD删除，再将其后的DVD前移一位
                for (int j = i; j < (integer == null ? dvds.length - 1 : integer); j++) {
                    dvds[j] = dvds[j + 1] ;
                }
//                将最后一位置空
                dvds[ dvds.length-1 ] = null;
                return true;
            }
        }

        return false;
    }

    /**
     *返回DVD[] dvds中第一个为null的元素的索引
     * @return   null说明dvds已满，Integer第一未null的元素的索引
     */
    private Integer returnIndex() {
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i] == null){
                return i;
            }
        }
        return null;
    }
}
